package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import beans.Client;

public class DAOClient extends DAO<Client> {

	Connection laCo = null;
	PreparedStatement leStm = null;
	ResultSet result = null;
	
	@Override
	public Client create(Client obj) {
		try {
			laCo = dataSource.getConnection();
			
			leStm=laCo.prepareStatement("INSERT INTO client (NOM, MDP) VALUES (?, ?)");
			leStm.setString(1, obj.getNom());
			leStm.setString(2, obj.getMdp());
			
			leStm.executeUpdate();
			
			leStm.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(laCo != null) laCo.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return obj;
	}

	@Override
	public Client update(Client obv) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(Client obv) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Client find(long id) {
		Client ret = null;
		try {
			laCo = dataSource.getConnection();
			
			leStm=laCo.prepareStatement("SELECT id, nom, mdp, estAdmin FROM client WHERE id=?");
			leStm.setLong(1, id);
			result = leStm.executeQuery();
		
			if(result.next()) {
				ret = new Client(result.getInt(1), result.getString(2), result.getString(3), result.getInt(4)==1);
			}
			
			leStm.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(laCo != null) laCo.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}
	
	public Client find(String nom) {
		Client ret = null;
		try {
			laCo = dataSource.getConnection();
			
			leStm=laCo.prepareStatement("SELECT id, nom, mdp, estAdmin FROM client WHERE nom=?");
			leStm.setString(1, nom);
			result = leStm.executeQuery();
		
			if(result.next()) {
				ret = new Client(result.getInt(1), result.getString(2), result.getString(3), result.getInt(4)==1);
			}
			
			leStm.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(laCo != null) laCo.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}

	@Override
	public List<Client> findAll() {
		List<Client> ret = null;
		try {
			laCo = dataSource.getConnection();
			
			leStm=laCo.prepareStatement("SELECT id, nom, prix FROM pizza");
			
			result = leStm.executeQuery();
			
			ret = new ArrayList<Client>();
			
			while(result.next()) {
				ret.add(new Client(result.getInt(1), result.getString(2), result.getString(3)));
			}
			
			leStm.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if(laCo != null) laCo.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return ret;
	}

}
